<template>
  <BaseDialog :loading="loading" destroy-on-close @confirm="save" @close="close" confirmText="保存" v-model="isShow"
              :title="title">
    <el-form
        ref="formRef"
        :model="state.dataForm"
        :rules="rules"
        label-width="80px"
        @submit="save"
    >
                   
                  <el-form-item label="职位名称" prop="postName">
                      <el-input clearable placeholder="请输入职位名称" v-model="state.dataForm.postName"/>
                  </el-form-item>
               
                  <el-form-item label="职位编号" prop="postCode">
                      <el-input clearable placeholder="请输入职位编号" v-model="state.dataForm.postCode"/>
                  </el-form-item>
               
                  <el-form-item label="是否启用" prop="enabled">
                     <el-switch :active-value="1" :inactive-value="2" v-model="state.dataForm.enabled"  />
                  </el-form-item>
               
                 <el-form-item label="顺序字段" prop="orderNum">
                     <el-input-Number :min="1"  :max="999999999" v-model="state.dataForm.orderNum" />
                 </el-form-item>
               
      

    </el-form>
  </BaseDialog>
</template>

<script setup lang="ts">

import {reactive, ref} from "vue";
import {PostModel} from "/@/api/system/model/PostModel";
import {insertPost, updatePost} from "/@/api/system/PostApi";

const emit = defineEmits([ 'load'])
const state = reactive({
  dataForm: {
           postId: '',
           postName: '',
           postCode: '',
           enabled: 1,
           orderNum: 1,
  } as PostModel
})

const formRef = ref()
const loading = ref(false)
const isShow = ref(false)
const title = ref("")
const init = (titleValue: string, data: PostModel) => {
  if (data) {
    state.dataForm = data
  }
  title.value = titleValue
  isShow.value = true
}
/**
 * 保存职位
 */
const save = async () => {
 await formRef.value.validate()
 loading.value = true
 if(state.dataForm.postId) {
   await updatePost(state.dataForm,()=>{
     loading.value = false
   })
 } else {
   await insertPost(state.dataForm,()=>[
     loading.value = false
   ])
 }
 emit('load')
 close()
}
/**
 * 关闭
 */
const close = () => {
  state.dataForm = {
           postId: '',
           postName: '',
           postCode: '',
           enabled: 1,
           orderNum: 1,
  } as PostModel
  isShow.value = false;
}

defineExpose({init})

const rules = {
         postId: [
             { required: true, message: '职位Id不能为空', trigger: 'blur' },
         ],
         postName: [
             { required: true, message: '职位名称不能为空', trigger: 'blur' },
             { min: 1, max: 64, message: '职位名称长度为1到64个字符',trigger: 'blur'}
         ],
         postCode: [
             { required: true, message: '职位编号不能为空', trigger: 'blur' },
             { min: 1, max: 64, message: '职位编号长度为1到64个字符',trigger: 'blur'}
         ],
         enabled: [
             { required: true, message: '是否启用(1:是/2:否)不能为空', trigger: 'blur' },
         ],
         orderNum: [
         ],
}
</script>

<style scoped>

</style>